// pages/myOrder/myOrder.js
Page({
  data: {
    tabs:['待上餐','待评价','已完成','已取消'],
    currentTab: 0
  },
  onLoad(options) {
    this.getOrder(0)
  },
  // 选中顶部导航栏
  selectTab(e){
    console.log('用户点击了',e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index
    this.setData({
      currentTab:index
    })
    if(index == 3){  //已取消的订单
      index = -1
    }
    console.log('当前订单状态值：',index);
    this.getOrder(index)
  },
  getOrder(status){
    wx.cloud.database().collection('order').where({
      status: status  //-1订单取消,0新下单待上餐,1待用户评价,2订单已完成
    }).orderBy('time', 'desc')
    .get()
    .then(res=>{
      console.log('请求到的订单',res);
      this.setData({
        list:res.data
      })
    })
    .catch(res=>{
      console.log('请求订单失败',res);
    })
  },
  cancel(e){
    console.log('id是',e.currentTarget.dataset.id);
    let id = e.currentTarget.dataset.id
    wx.cloud.database().collection('order').doc(id)
    .update({
      data:{
        status: -1 //-1订单取消,0新下单待上餐,1待用户评价,2订单已完成
      }
    }).then(res=>{
      console.log('取消订单',res);
      this.getOrder(0)
    })
    .catch(res=>{
      console.log('取消订单失败',res);
    })
  },
  //去评价
  comment(e){
    let id = e.currentTarget.dataset.id
    let user = wx.getStorageSync('user_info_cache')
    console.log('用户信息',user);
    wx.showModal({
      title: '输入评价内容',
      content: '',
      editable:true,
      complete: (res) => {
        if (res.cancel) {
          console.log('用户点击取消',res);

        }
    
        if (res.confirm) {
          console.log('用户点击确定',res);
          console.log('用户输入内容',res.content);
          if(res.content&&res.content.length>0){
            wx.cloud.database().collection('comment').add({
              data:{
                name:user.nickName,
                orderId:id,
                avatarUrl:user.avatarUrl,
                content: res.content,
                time:getApp().getTime()
              }
            }).then(res=>{
              console.log('评价成功',res);
            })
          }else{
           wx.showToast({
             title: '请输入评价',
             icon:'error'
           })     
          }
        }
      }
    })
  },
  // 去我的评价页
  goMyComment(){
    wx.navigateTo({
      url: '/pages/myComment/myComment',
    })
  },
  copyBtn(e){
    console.log(e);
    wx.setClipboardData({
      data: e.currentTarget.dataset.id,
    })
  },
 
})